package me.bcloud.web.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Queue;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.bcloud.web.dao.UserDao;
import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.User;
import me.bcloud.web.domain.WorkUser;
import me.bcloud.web.spring.Cache;
import me.bcloud.web.util.Tools;

import org.springframework.stereotype.Controller;

@Controller("data")
public class DataServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name = "userDaoMysqlJdbcImpl")
	private UserDao userDao;
	@Resource
	private Cache cache;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		
		JobConfig jobConfig = cache.getJobMap().get((Integer)req.getSession().getAttribute("job"));
		
		if (null == jobConfig) {
			resp.getOutputStream().write("NO_DATA".getBytes());
			return;
		}

		List<User> users = getData(jobConfig);
		if (null == users) {
			resp.getOutputStream().write("NO_DATA".getBytes());
			return;
		}
		resp.getOutputStream().write(Tools.usersToStr(users));
	}

	private List<User> getData(JobConfig jobConfig) {
		/**
		 * 取出缓存用户
		 */
		Queue<User> users = cache.getCacheMap().get(jobConfig.getId());

		HashMap<Integer, WorkUser> workUsers = cache.getWorkMap().get(jobConfig.getId());

		List<User> ready2workUsers = new ArrayList<User>();

		synchronized (users) {

			if (users.isEmpty()) {
				return null;
			}

			/**
			 * 如果不够 加载缓存
			 */

			if (users.size() < jobConfig.getCacheNum() / 2
					&& jobConfig.isOpen()) {

				List<User> newusers = userDao.findAll(jobConfig);

				users.addAll(newusers);

			}

			/**
			 * 将要分发用户个数如果大于总缓存数 则全部取完 不会越界
			 */
			int readnum = jobConfig.getRunNum();
			if (readnum > users.size()) {
				readnum = users.size();
			}
			/**
			 * 取出工作用户从缓存 移除工作用户 将用户封装成工作用户添加到 工作用户队列里
			 */

			for (int i = 0; i < readnum; i++) {
				User user = users.poll();
				WorkUser workUser = new WorkUser();
				workUser.setUser(user);
				workUser.setWorktime(new Date(System.currentTimeMillis()));
				workUsers.put(user.getId(),workUser);
				ready2workUsers.add(user);
			}
		}
		System.out.println(jobConfig.getGname() + " users: " + users.size());
		System.out
				.println(jobConfig.getGname() + " works: " + workUsers.size());

		return ready2workUsers;

	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}

	public Cache getCache() {
		return cache;
	}

}
